import axios from 'axios'
let locales = [
  '/ar',
  '/de',
  '/es',
  '/fr',
  '/hi',
  '/id',
  '/it',
  '/ja',
  '/ko',
  '/ms',
  '/my',
  '/nl',
  '/pt',
  '/ro',
  '/ru',
  '/th',
  '/tl',
  '/vi',
  '/zh',
  '/zh-tw',
  ''
]
export default {
  path: '/sitemap.xml',
  hostname: 'https://www.sealmg.com/',
  cacheTime: 1000 * 60 * 60 * 24,
  defaults: {
    exclude: true,
    changefreq: 'always',
    lastmod: new Date()
  },
  exclude: ['/**'],
  etag: true,
  sitemaps: locales.flatMap(locale => {
    const localePrefix = locale === '' ? 'root' : locale.replace('/', '')
    const sections = [
      {
        name: 'gameCollection',
        path: `/sitemap-${localePrefix}-game-collection.xml`,
        fetchData: async () => {
          try {
            const gameDataRes = await axios.get('https://api.sealmg.com/api/sitemap/lists?type=game')
            if (!gameDataRes?.data?.data?.length) {
              return []
            }
            return gameDataRes.data.data.map(i => ({
              url: `${locale}/game/${i.code}`,
              lastmod: new Date().toISOString().split('T')[0],
              changefreq: 'always'
            }))
          } catch (e) {}
        }
      },
      {
        name: 'cardCollection',
        path: `/sitemap-${localePrefix}-card-collection.xml`,
        fetchData: async () => {
          try {
            const gameDataRes = await axios.get('https://api.sealmg.com/api/sitemap/lists?type=cards-category')
            if (!gameDataRes?.data?.data?.length) {
              return []
            }
            return gameDataRes.data.data.map(i => ({
              url: `${locale}/cards/${i.code}`,
              lastmod: new Date().toISOString().split('T')[0],
              changefreq: 'always'
            }))
          } catch (e) {}
        }
      },
      {
        name: 'rechargeCollection',
        path: `/sitemap-${localePrefix}-recharge-collection.xml`,
        fetchData: async () => {
          try {
            const gameDataRes = await axios.get('https://api.sealmg.com/api/sitemap/lists?type=recharge-category')
            if (!gameDataRes?.data?.data?.length) {
              return []
            }
            return gameDataRes.data.data.map(i => ({
              url: `${locale}/top-up/${i.code}`,
              lastmod: new Date().toISOString().split('T')[0],
              changefreq: 'always'
            }))
          } catch (e) {}
        }
      },
      {
        name: 'rechargeCollection',
        path: `/sitemap-${localePrefix}-recharge-game.xml`,
        fetchData: async () => {
          try {
            const cardDataRes = await axios.get('https://api.sealmg.com/api/sitemap/lists?type=card')
            if (!cardDataRes?.data?.data?.length) {
              return []
            }
            return cardDataRes.data.data.map(i => ({
              url: `${locale}/product/${i.code}`,
              lastmod: new Date().toISOString().split('T')[0],
              changefreq: 'always'
            }))
          } catch (e) {}
        }
      },
      {
        name: 'recharge',
        path: `/sitemap-${localePrefix}-recharge.xml`,
        fetchData: async () => {
          try {
            const rechargeDataRes = await axios.get('https://api.sealmg.com/api/sitemap/lists?type=recharge')
            if (!rechargeDataRes?.data?.data?.length) {
              return []
            }
            return rechargeDataRes.data.data.map(i => ({
              url: `${locale}/product/${i.code}`,
              lastmod: new Date().toISOString().split('T')[0],
              changefreq: 'always'
            }))
          } catch (e) {}
        }
      },
      {
        name: 'gameCategory',
        path: `/sitemap-${localePrefix}-game-category.xml`,
        fetchData: async () => {
          try {
            const gameCategoryRes = await axios.get('https://api.sealmg.com/api/goods_category/index?type=0')
            if (!gameCategoryRes?.data?.data?._child?.length) {
              return []
            }
            return gameCategoryRes.data.data._child.map(i => ({
              url: `${locale}/game-category/${i.code}`,
              lastmod: new Date().toISOString().split('T')[0], //
              changefreq: 'always'
            }))
          } catch (e) {}
        }
      },
      {
        name: 'cardCategory',
        path: `/sitemap-${localePrefix}-card-category.xml`,
        fetchData: async () => {
          try {
            const cardCategoryRes = await axios.get('https://api.sealmg.com/api/goods_category/index?type=1')
            if (!cardCategoryRes?.data?.data?._child?.length) {
              return []
            }
            return cardCategoryRes.data.data._child.map(i => ({
              url: `${locale}/cards-category/${i.code}`,
              lastmod: new Date().toISOString().split('T')[0],
              changefreq: 'always'
            }))
          } catch (e) {}
        }
      },
      {
        name: 'rechargeCategory',
        path: `/sitemap-${localePrefix}-recharge-category.xml`,
        fetchData: async () => {
          try {
            const rechargeCategoryRes = await axios.get('https://api.sealmg.com/api/goods_category/index?type=2')
            if (!rechargeCategoryRes?.data?.data?._child?.length) {
              return []
            }
            return rechargeCategoryRes.data.data._child.map(i => ({
              url: `${locale}/top-up-category/${i.code}`,
              lastmod: new Date().toISOString().split('T')[0],
              changefreq: 'always'
            }))
          } catch (e) {}
        }
      },
      {
        name: 'news',
        path: `/sitemap-${localePrefix}-news.xml`,
        fetchData: async () => {
          try {
            const newDataRes = await axios.get('https://api.sealmg.com/api/sitemap/lists?type=news')
            if (!newDataRes?.data?.data?.length) {
              return []
            }
            return newDataRes.data.data.map(i => ({
              url: `${locale}/news/${i.code}`,
              lastmod: new Date().toISOString().split('T')[0],
              changefreq: 'always'
            }))
          } catch (e) {}
        }
      }
    ]
    return sections.map(section => ({
      path: section.path,
      routes: section.fetchData,
      exclude: ['/**']
    }))
  })
}
